function refreshSize() {
    var width = document.body.clientWidth;
    var content = document.getElementById('content');
    var right = document.getElementById('right');
    var left = document.getElementById('left');

    if (width < 720) {
        right.style = "display:none;";
        left.style = "margin-right: 0";
    } else {
        right.style = "";
        left.style = "margin-right: 400px;";
    }

}

function getParam(param) {
    let href = location.href;
    href = href.substring(href.indexOf("?") + 1);
    let params = href.split('&');
    for (let i = 0; i < params.length; i++) {
        let d = params[i].split('=');
        if (d.length === 2 && d[0] === param) {
            return d[1];
        }
    }
}

window.onresize = function () {
    refreshSize();
}


refreshSize();

let page = getParam('archive');
if (page != undefined && page !== '' && page.match(/\d+/)) {
    let url = location.origin;
    let path = location.pathname;
    onReading(url + path + 'index.php/archives/' + page);
}

var load_progress;

function onRead(link) {
    let page = link.match(/\/index.php\/archives\/(\d+)\//)[0].replace(/\/index.php\/archives\/(\d+)\//, "$1");
    let url = location.origin;
    let path = location.pathname;
    location.href = url + path + '?archive=' + page;
}


function onReading(link) {
    $.ajax({
        url: link,
        success: function (t) {
            document.documentElement.scrollTop = 0;
            $('#left').html(t);
            load_more();
        }
    });
}

var more_content_link;

// //加载更多
function load_more() {
    try {
        var more_content = document.getElementById('more_content');
        var load_content = document.getElementById('load_content');
        var nodes = more_content.childNodes;
        nodes.forEach(function (v) {
            if (v.innerText === "下一页") {
                more_content.removeChild(v);
                more_content_link = v.href;
                load_content.style.display = '';
            }
        })


        load_content.onclick = function () {
            load_progress = mdui.snackbar({
                timeout: 0,
                message: '加载中...',
                position: 'bottom'
            });
            load_content.style.display = 'none';
            var currentContent = document.getElementById('content-body');
            $.ajax({
                url: more_content_link,
                type: 'get',
                success: function (data) {
                    var xml = new DOMParser().parseFromString(data, 'text/html');
                    var nodes = xml.getElementById('left').childNodes;
                    nodes.forEach(function (v) {
                        if (v.id === 'content-body') {
                            currentContent.appendChild(v);
                        } else if (v.id === "more_content") {
                            let nodess = v.childNodes;
                            nodess.forEach(function (v) {
                                // console.log(v);
                                if (v.innerText === "下一页") {
                                    more_content_link = v.href;
                                    load_content.style.display = '';
                                }
                            })
                        }
                        load_progress.close();
                    })
                },
                error: function (err) {
                    alert(err);
                    load_progress.close();
                }
            });

        }
    } catch (e) {

    }


}

if (location.href.indexOf("archive") === -1)
    load_more();

